package com.hxy.manager.activity.goods;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.text.Editable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.hxy.manager.R;
import com.hxy.manager.activity.CaptureActivity;
import com.hxy.manager.dao.DBManager;
import com.hxy.manager.entity.GoodsEntity;
import com.hxy.manager.entity.GoodsEntityDao;
import com.hxy.manager.service.GoodsEntityService;
import com.hxy.manager.util.Constant;
import com.hxy.manager.util.StringUtil;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import static com.hxy.manager.service.GoodsEntityService.postToServer;

/**
 * Created by admin-home on 2017/12/31.
 */

public class GoodsAddActivity extends Activity {

    EditText editText0;
    EditText editText1;
    EditText editText2;
    EditText editText3;
    EditText editText4;
    EditText editText5;
    EditText editDateEditText;

    public static GoodsAddActivity goodsAddActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add);
        setTitle("添加");
        goodsAddActivity = this;
        getActionBar().setDisplayHomeAsUpEnabled(true);//向上返回
        editText0 = (EditText) findViewById(R.id.editText0);
        editText1 = (EditText) findViewById(R.id.editText1);
        editText2 = (EditText) findViewById(R.id.editText2);
        editText3 = (EditText) findViewById(R.id.editText3);
        editText4 = (EditText) findViewById(R.id.editText4);
        editText5 = (EditText) findViewById(R.id.editText5);
        editDateEditText = (EditText) findViewById(R.id.editDateEditText);
        Calendar now = Calendar.getInstance();
        editDateEditText.setText(now.get(Calendar.YEAR) + "年" + (now.get(Calendar.MONTH) + 1) + "月" + now.get(Calendar.DAY_OF_MONTH) + "日");

        Intent intent = getIntent();
        String scanCode = intent.getStringExtra(Constant.SCAN_CODE);


        if (scanCode != null) {
//            先查询数据库是否已经存在了
            GoodsEntity oneByNumber = GoodsEntityService.getOneByNumber(this, scanCode);
            if (oneByNumber != null) {

                Intent intent2 = new Intent(GoodsAddActivity.this, GoodsShowActivity.class);
                intent2.putExtra("scanCode", scanCode);
                startActivity(intent2);
                finish();

            } else {
                editText0.setTextColor(Constant.RGB_RED);
                editText0.setText(scanCode);
            }
        }

        Button scan = (Button) findViewById(R.id.scan);
        scan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (ContextCompat.checkSelfPermission(GoodsAddActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(GoodsAddActivity.this, new String[]{Manifest.permission.CAMERA}, 1);
                } else {
                    startActivityForResult(new Intent(GoodsAddActivity.this, CaptureActivity.class), 0);
                }
                //  Intent intent = new Intent(Add.this, CaptureActivity.class);
                //   startActivityForResult(intent , 0x1);
            }
        });
        Button save = (Button) findViewById(R.id.save);

        final Context context = this;

        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Editable numberEditText = editText0.getText();
                if (numberEditText.toString().equals("")) {
                    //  if(editText0.getText().toString().equals("")||editText1.getText().toString().equals("")||editText2.getText().toString().equals("")||editText3.getText().toString().equals("")||editText4.getText().toString().equals("")||editText4.getText().toString().equals("")||editText5.getText().toString().equals("")||editText6.getText().toString().equals("")) {
                    Toast.makeText(GoodsAddActivity.this, "条形码不能为空！", Toast.LENGTH_SHORT).show();
                    // TODO 查询是否存在了


                } else if (editText1.getText().toString().equals("")) {
                    //  if(editText0.getText().toString().equals("")||editText1.getText().toString().equals("")||editText2.getText().toString().equals("")||editText3.getText().toString().equals("")||editText4.getText().toString().equals("")||editText4.getText().toString().equals("")||editText5.getText().toString().equals("")||editText6.getText().toString().equals("")) {

                    Toast.makeText(GoodsAddActivity.this, "名称不能为空！", Toast.LENGTH_SHORT).show();

                } else if (StringUtil.isEmpty(editText2.getText().toString())) {
                    Toast.makeText(GoodsAddActivity.this, "价格不能为空！", Toast.LENGTH_SHORT).show();
                } else {

                    GoodsEntity goodsEntity = new GoodsEntity();

                    goodsEntity.setNumber(editText0.getText().toString());
                    goodsEntity.setName(editText1.getText().toString());
                    goodsEntity.setPrice(editText2.getText().toString());
                    goodsEntity.setPress(editText3.getText().toString());
                    goodsEntity.setDate(editText4.getText().toString());
                    goodsEntity.setAuthor(editText5.getText().toString());
                    goodsEntity.setLocation(editDateEditText.getText().toString());

                    Date d = new Date();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
                    String dateNowStr = sdf.format(d);

                    goodsEntity.setUpdateTime(System.currentTimeMillis());


                    long id = GoodsEntityService.save(context, goodsEntity);
                    // 同步到线上服务器
                    postToServer(context, goodsEntity);
                    goodsEntity.setId(id);
                    if (id >= 0) {
//                        Toast.makeText(GoodsAddActivity.this, "添加成功！", Toast.LENGTH_SHORT).show();
                        editText0.setText("");
                        editText1.setText("");
                        editText2.setText("");
                        editText3.setText("");
                        editText4.setText("");
                        editText5.setText("");
                    }

                }

            }
        });


    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && data != null) {
            Bundle bundle = data.getExtras();
            String scanCode = bundle.getString("result");
            GoodsEntityDao goodsEntityDao = DBManager.getReadSession(this).getGoodsEntityDao();
            GoodsEntity user = goodsEntityDao.queryBuilder().where(GoodsEntityDao.Properties.Number.eq(scanCode)).unique();

            if (user == null) {
                editText0.setText(bundle.getString("result"));
                editText0.setTextColor(0xFFFF0000);
                editText0.setTextSize(20);
            } else {
                Intent intent = new Intent(GoodsAddActivity.this, GoodsShowActivity.class);
                intent.putExtra("scanCode", scanCode);
                startActivity(intent);
                finish();
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //  getMenuInflater().inflate(R.menu.main,menu);
        return super.onCreateOptionsMenu(menu);
    }


    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case android.R.id.home:  //默认添加的
                finish();
                break;
        }
        return false;
    }


}
